/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tpiServeur;

import tpiprotocole.LH;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Calendar;
import java.util.Locale;

/**
 * @date 16 mai 2011
 * @time 10:44:29
 * @author Luis.HAFNER
 */
class ConnexionServeur implements Runnable {
    private final Socket socket;
    private final ControleurServeur controleurServeur;
    private boolean estEnregistre= false;
    private String nomClient;

    public ConnexionServeur(Socket socket,ControleurServeur controleurServeur) {
        this.socket = socket;
        this.controleurServeur = controleurServeur;
    }

    public void run() {
        BufferedReader reception = null;
        try {
            reception = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            // Ecriture du socket
            PrintWriter emission = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
            String requete = reception.readLine();
            
            if (requete != null && requete.startsWith("LH")) {
                // Supprimer la partie du protocole pour simplifier la chaine
                requete = requete.substring(2);
                if (requete.startsWith("100"))
                     if (this.controleurServeur.getModeleServeur().inscrireClient(requete.substring(3))){
                        nomClient = requete.substring(3);
                        emission.println(LH.Connecter_OK());
                        this.estEnregistre=true;
                    }
                    else
                         emission.println(LH.Connecter_KO());
                while(this.estEnregistre){
                    requete = reception.readLine();
                    if (requete != null && requete.startsWith("LH")){
                        requete = requete.substring(2);
                        if(requete.startsWith("200")){
                            this.controleurServeur.getModeleServeur().desinscrireClient(nomClient);
                            this.estEnregistre=false;
                            reception.close();
                            emission.close();
                            socket.close();
                        }
                        else if(requete.startsWith("300")){

                        Calendar cal = Calendar.getInstance();

                        StringBuilder sbReception = new StringBuilder();
                        sbReception.append("[");
                        sbReception.append(cal.get(Calendar.DAY_OF_MONTH));
                        sbReception.append(" ");
                        sbReception.append(cal.getDisplayName(Calendar.MONTH,1 , Locale.ENGLISH));
                        sbReception.append(" ");
                        sbReception.append(cal.get(Calendar.YEAR));
                        sbReception.append(" ");
                        sbReception.append(cal.get(Calendar.HOUR_OF_DAY));
                        sbReception.append(":");
                        sbReception.append(cal.get(Calendar.MINUTE));
                        sbReception.append(":");
                        sbReception.append(cal.get(Calendar.SECOND));
                        sbReception.append("/");
                        sbReception.append(this.nomClient);
                        sbReception.append("] ");
                        sbReception.append(requete.substring(3));
                        
                        this.controleurServeur.getVueServeur().getReception().append(sbReception.toString()+"\n");
                        this.controleurServeur.getFichierLog().seek(this.controleurServeur.getFichierLog().length());
                        this.controleurServeur.getFichierLog().writeBytes(sbReception.toString()+"\r\n");

                        }
                    }
                }
            }
        }
        catch (IOException ex) {
            System.out.println(ex);
        }
    }
}
